

#include "atomic_op_namespace.h"

#include <sys/atomic.h>

#ifdef __HAVE_ATOMIC64_OPS

uint64_t fetch_and_add_8(volatile uint64_t *, uint64_t, ...)
    asm("__sync_fetch_and_add_8");

uint64_t
fetch_and_add_8(volatile uint64_t *addr, uint64_t val, ...)
{
	uint64_t old, new;

	do {
		old = *addr;
		new = old + val;
	} while (atomic_cas_64(addr, old, new) != old);
	return old;
}

void
atomic_add_64(volatile uint64_t *addr, int64_t val)
{
   (void) fetch_and_add_8(addr, val);
}

__strong_alias(__atomic_fetch_add_8,__sync_fetch_and_add_8)

#undef atomic_add_64
atomic_op_alias(atomic_add_64,_atomic_add_64)

#if defined(_LP64)
#undef atomic_add_long
atomic_op_alias(atomic_add_long,_atomic_add_64)
__strong_alias(_atomic_add_long,_atomic_add_64)

#undef atomic_add_ptr
atomic_op_alias(atomic_add_ptr,_atomic_add_64)
__strong_alias(_atomic_add_ptr,_atomic_add_64)
#endif /* _LP64 */

#endif
